import time

from selenium.webdriver.common.by import By

from common.utils import chromeDriver


class Registration:
    driver = ""
    url = ""

    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/register"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)

    #注册测试
    def RegistrationTest(self, name, username, password, repassword):
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#rePassword"))

        #输入姓名, 登录账号，密码，密码确认
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys(name)
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(username)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(password)
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(repassword)

        #点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()

        #添加屏幕截图
        chromeDriver.getScreenShot()

    #注册成功测试
    def RegisteredSuccessfully(self):
        self.registrationTest("li209779", "li209779", "123456", "123456")

        time.sleep(1)
        print(self.driver.current_url, "---------------------------", self.url)
        if self.driver.current_url == self.url:
            text = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div").text

            print("账号注册失败，原因是：", text)
        else:
            print("账号注册成功")

        # 注册失败测试
    def LoginHasFailed(self):
        # 测试用例1，输入登录账号、密码、密码确认，点击注册账户按钮，提示"用户名是必填字段!"
        self.RegistrationTest("", "Aurora", "123321", "123321")
        text = self.driver.find_element(By.CSS_SELECTOR, "#name_help > div").text
        assert text == "用户名是必填项！"
        # 测试用例2，输入例如已经存在的账号进行注册，
        self.RegistrationTest("Aurora", "Aurora", "123321", "123321")
        text = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div").text
        assert text == "账号已存在"
        # 测试用例3，密码和确认密码不一致
        self.RegistrationTest("Aurora", "Aurora", "123322", "123321")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword_help > div")